<?php

class action
{
    private $user_id = 0;
    private $user_info = array();

    public function __construct() {
        $this->token = an_request('token');
        if ($this->token) {
            $user_info = user_helper::get_user_info_by_token($this->token);
            if ($user_info && $user_info['status'] == 1) {
                $this->user_id = $user_info['id'];
                $this->user_info = $user_info;
            }
        }
    }

    /**
     * 获取任务列表
     *
     * @throws AnException
     */
    public function get_job_list() {
        $agency_id = user_helper::get_dealer_id_by_user_id($this->user_id);
        $job_list = topic_helper::get_actived_job_list(array('agency_id' => $agency_id));
        api_helper::return_data(0, 'success', array('job_list' => $job_list));
    }

    /**
     * 获取门店列表
     *
     * @throws AnException
     */
    public function get_store_list() {

        if ($this->user_id == 0) {
            api_helper::return_data(1, '身份错误');
        }

        api_helper::check_sign(array());

        $role_id = user_helper::get_user_role_id($this->user_id);

        // 市场人员或者合伙人对应的自己的门店
        switch ($role_id) {
            case user_config::AGENCY:
                $user_store_list = _model('user_store')->getList(array('agency_id' => $this->user_id));
                $store_list = _model('store')->getList(array('agency_id' => $this->user_id));
                break;
            case user_config::MARKETING:
                $user_store_list = _model('user_store')->getList(array('marketing_id' => $this->user_id));
                $store_list = _model('store')->getList(array('marketing_id' => $this->user_id));
                break;
        }
        // 市场人员对应的合伙人旗下所有门店
        //        $user_store_list = _model('user_store')->getList(array('agency_id' => $agency_id));
        //        $store_list = _model('store')->getList(array('agency_id' => $agency_id));

        $sl_index = array();
        foreach ($store_list as $store) {
            $sl_index[$store['owner_id']][] = parse_param(array(
                'id' => '',
                'title' => '',
                'title_branch' => '',
            ), $store);
        }

        $S = array();
        foreach ($user_store_list as $user_store) {
            if (isset($sl_index[$user_store['user_id']])) {
                $S[] = parse_param(array(
                    'id' => '',
                    'title' => '',
                    'store_num' => '',
                    'store_list' => '',
                ), $user_store, array(
                    'store_num' => count($sl_index[$user_store['user_id']]),
                    'store_list' => $sl_index[$user_store['user_id']],
                ));
            }
        }
        $ret = array();

        $ret['user_store_list'] = $S;

        //        $user_info = user_helper::get_user_info($this->user_id);

        api_helper::return_data(0, 'success', $ret);
    }

    /**
     * 获取当前任务完成状态
     * @throws AnException
     */
    public function get_job_comply_info() {

        if ($this->user_id == 0) {
            api_helper::return_data(1, '身份错误');
        }

        $job_id = an_request('job_id', 0);

        $sign = array(
            'job_id' => $job_id,
        );

        api_helper::check_sign($sign);

        $uploader_id = $this->user_id;
        $role_id = user_helper::get_user_role_id($uploader_id);

        if ($role_id == user_config::MARKETING) {
            $agency_id = user_helper::get_dealer_id_by_marketing_id($uploader_id);
            $store_ids = store_helper::get_store_ids_by_marketing_id($uploader_id);
        } else {
            $agency_id = $this->user_id;
            $store_ids = store_helper::get_store_ids_by_agency_id($uploader_id);
        }

        // 总数
        $total = _model('topic_job_comply')->getTotal(array(
            'job_id' => $job_id,
            'parent_id' => $agency_id
        ));

        $passed = _model('topic_job_comply')->getTotal(array(
            'job_id' => $job_id,
            'parent_id' => $agency_id,
            'status' => 1,
        ));

        $ret = array(
            'total' => $total,
            'passed' => $passed,
            'store_comply_list' => array(),
        );

        if ($total) {

            $job_list = topic_helper::get_job_list();

            // 任务列表
            $store_comply_list = _model('topic_job_comply')->getList(array(
                'job_id' => $job_id,
                'parent_id' => $agency_id,
                'store_id' => $store_ids,
            ));

            //门店列表
            $store_list = api_helper::array_to_filelds_assoc('id', _model('store')->getList(array('id' => $store_ids)));

            if (!empty($store_comply_list)) {

                $agency_info = user_helper::get_user_info($agency_id);

                foreach ($store_comply_list as $key => $item) {

                    $store_comply_list[$key]['agency_name'] = $agency_info['realname'];

                    $uploader_info = user_helper::get_user_info($item['uploader_id']);
                    $store_comply_list[$key]['uploader_name'] = $uploader_info['realname'];

                    $store_info = $store_list[$item['store_id']];
                    $store_comply_list[$key]['title'] = $store_info['title'];
                    $store_comply_list[$key]['title_branch'] = $store_info['title_branch'];
                    $job_info = $job_list[$item['job_id']];
                    $store_comply_list[$key]['job_name'] = $job_info['name'];

                    // 处理图片
                    $pics = json_decode($item['pics'], true);
                    $store_comply_list[$key]['pic_num'] = count($pics);
                    foreach ($pics as $k => $path) {
                        $pics[$k] = _image($path);
                    }
                    $store_comply_list[$key]['pics'] = $pics;
                }
                $ret['store_comply_list'] = $store_comply_list;
            }
        }

        api_helper::return_data(0, 'success', $ret);
    }

    /**
     * 提交任务图片
     */
    public function submit_job_comply() {

        if ($this->user_id == 0) {
            api_helper::return_data(1, '身份错误');
        }

        $job_id = an_request('job_id', '0');
        $store_id = an_request('store_id', '0');
        $pics = an_request('pics', '', 'request', 0);
        $extra = an_request('extra', '');

        $data = array(
            'job_id' => $job_id,
            'store_id' => $store_id,
            'pics' => $pics,
            'extra' => $extra,
            'upload_time' => datetime(),
        );

        $sign = parse_param(array(
            'job_id' => '0',
            'store_id' => '0',
            'pics' => '',
            'extra' => '',
        ), $data);

        api_helper::check_sign($sign);

        if ($job_id == 0) {
            api_helper::return_data(1, '请选择任务');
        }

        if ($store_id == 0) {
            api_helper::return_data(1, '请选择门店');
        }

        $pic_list = json_decode($pics, 1);

        if (empty($pic_list)) {
            api_helper::return_data(1, '至少上传一张图片');
        }

        // 处理图片
        foreach ($pic_list as $p => $pic) {
            $pic_list[$p] = $pic['img'];
        }
        $data['pics'] = json_encode($pic_list);

        $uploader_id = $this->user_id;
        $role_id = user_helper::get_user_role_id($uploader_id);

        $filter = parse_param(array(
            'job_id' => '',
            'store_id' => '',
        ), $data);
        $job_info = _model('topic_job_comply')->read($filter);
        if (!empty($job_info)) {
            if ($job_info['status'] == 2) { // 驳回的重新上传
                // 如果这张图片是上传者传的
                // TODO 合伙人覆盖
                if ($job_info['uploader_id'] === $uploader_id || $role_id === user_config::AGENCY) {
                    // 状态重置为未审核
                    $data['status'] = 0;
                    $data['uploader_id'] = $uploader_id;
                    $ret = _model('topic_job_comply')->update(array('id' => $job_info['id']), $data);
                }
            } else {
                api_helper::return_data(1, '此门店已经已经上传过图片');
            }
        } else {
            $agency_id = user_helper::get_dealer_id_by_user_id($uploader_id);

            $data['uploader_id'] = $uploader_id;
            $data['parent_id'] = $agency_id;
            // 创建
            $ret = _model('topic_job_comply')->create($data);
        }

        if ($ret) {
            api_helper::return_data(0, 'success');
        }

        api_helper::return_data(1, '上传失败');
    }
}